<?php

namespace web\api\controller;

/**
 * Description of News
 * 评论
 */
class Comments extends UserBase {

    /**
     * 评论列表
     */
    public function commentsList(){
        try {
            $video_id = $this->_post('video_id');//对应的视频ID
            $type = $this->_post('type');//评论地区类型  1视频
            $filter = ' a.is_delete=0 and a.video_id='.$video_id.' and a.type='.$type ;
            $order = ' a.id desc ';
            $fields = 'a.*,b.username,b.head_img';
            $m = new \addons\comments\model\Comments();
            $childM = new \addons\comments\model\CommentsChild();
            $list = $m->getCommentsList($this->getPageIndex(),$this->getPageSize(), $fields, $filter, $order);
            foreach ($list as &$v) {
                $child_filter = ' type= '.$type.' and comments_id = '.$v['id'];
                $fields = 'a.content,a.create_time,b.username,b.head_img';
                $v['child_list'] = $childM->getChildList(-1,-1, $fields, $child_filter, 'a.id desc')??[];

            }
            return $this->successJSON($list);
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 添加评论
     */
    public function commentsAdd(){
        $data['video_id'] = $this->getParamVal('video_id'); //视频ID
        $data['type'] = $this->getParamVal('type'); //1视频
        $data['content'] = trim($this->getParamVal('content'));
        $data['user_id'] = $this->user_id;
        $data['create_time'] = NOW_DATETIME;
        $m = new \addons\comments\model\Comments();
        try {
            $re_id = $m->add($data);
            if($re_id < 1) return $this->failJSON('评论失败');
            //增加视频评论数量
            $videoM = new \addons\video\model\Video();
            $videoM->updatePraiseCom($data['video_id'], 2, 1);
            return $this->successJSON();
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 添加子评论
     */
    public function commentsChildAdd(){
        $data['comments_id'] = $this->getParamVal('comments_id'); //评论ID
        $data['type'] = $this->getParamVal('type');
        $data['content'] = trim($this->getParamVal('content'));
        $data['user_id'] = $this->user_id;
        $data['create_time'] = NOW_DATETIME;
        $childM = new \addons\comments\model\CommentsChild();
        try {
            $re_id = $childM->add($data);
            if($re_id < 1) return $this->failJSON('评论失败');
            return $this->successJSON();
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

    /**
     * 逻辑删除评论
     */
    public function commentsDel(){
        try {
            $id = $this->getParamVal('id'); //评论id
            $type = $this->getParamVal('type');
            $m = new \addons\comments\model\Comments();
            $re_del = $m->where(['type' => $type, 'id' => $id, 'user_id' => $this->user_id])->update(['is_delete' => 1, 'update_time' => NOW_DATETIME]);
            if(!$re_del) return $this->failJSON('该评论不是您发布');
            $video_id = $m->getDetail($id, 'video_id')['video_id'];
            $videoM = new \addons\video\model\Video();
            $videoM->updatePraiseCom($video_id, 2, 2);
            return $this->successJSON();
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }

    }

    /**
     * 逻辑子删除评论
     */
    public function commentsChildDel(){
        try {
            $id = $this->getParamVal('id'); //子评论id
            $type = $this->getParamVal('type');
            $m = new \addons\comments\model\CommentsChild();
            $re_del = $m->where(['type' => $type, 'id' => $id, 'user_id' => $this->user_id])->delete();
            if(!$re_del) return $this->failJSON('该评论不是您发布');
            return $this->successJSON();
        } catch (\Exception $ex) {
            return $this->failJSON($ex->getMessage());
        }
    }

//    /**
//     * 视频点赞
//     */
//    public function commentsPraise(){
//        $type = $this->_post('type');
//        $video_id = $this->_post('video_id');//视频ID
//        $videoM = new \addons\video\model\Video();
//        $videoPraiseM = new \addons\video\model\VideoPraise();
//        $data = [
//            'type' => $type,
//            'video_id' => $video_id,
//            'user_id' => $this->user_id
//        ];
//        try {
//            $list = $videoPraiseM->where($data)->find();
//            if($list){
//                $re = $videoPraiseM->where($data)->delete();
//                if(!$re) return $this->failJSON("取消赞失败");
//                $videoM->updatePraiseCom($video_id, 1, 2);
//            }else{
//                $data['create_time'] = NOW_DATETIME;
//                $re = $videoPraiseM->add($data);
//                if(!$re) return $this->failJSON("点赞失败");
//                $videoM->updatePraiseCom($video_id, 1, 1);
//            }
//            return $this->successJSON();
//        } catch (\Exception $ex) {
//            return $this->failJSON($ex->getMessage());
//        }
//    }
    
}
